package com.wdd.recoveryvehicle.mapper;

import com.wdd.recoveryvehicle.vehicleInfo.bean.VehicleInfoVB;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 车辆信息
 *
 * @author globalMr
 */
@Repository
@Mapper
public interface VehicleInfoMapper {

    /**
     * 插入 车辆信息
     *
     * @param vehicleInfoVB
     * @return
     */
    @Insert(" insert into vehicle_info set " +
            " vehicle_id = #{vehicleInfoVB.vehicle_id}, vehicle_cph = #{vehicleInfoVB.vehicle_cph}, vehicle_cate = #{vehicleInfoVB.vehicle_cate}, vehicle_type = #{vehicleInfoVB.vehicle_type}, vehicle_frame = #{vehicleInfoVB.vehicle_frame}, " +
            " vehicle_factory = #{vehicleInfoVB.vehicle_factory}, vehicle_model = #{vehicleInfoVB.vehicle_model}, vehicle_status = #{vehicleInfoVB.vehicle_status}, create_time = now(), remark = #{vehicleInfoVB.remark}," +
            " create_company = #{company_id}, create_user = #{user_id}, production_date = #{vehicleInfoVB.production_date} ")
    boolean insertVehicleInfo(@Param("vehicleInfoVB") VehicleInfoVB vehicleInfoVB, @Param("user_id") String user_id, @Param("company_id") String company_id);

    /**
     * 插入 车和人的关系信息表
     * @param emp_id
     * @param vehicle_id
     * @return
     */
    @Insert(" insert into emp_vehicle_relation set " +
            " emp_id = #{emp_id}, vehicle_id = #{vehicle_id} ")
    boolean insertVehicleEmpRelation(@Param("emp_id") String emp_id, @Param("vehicle_id") String vehicle_id);


    /**
     * 查询 车辆信息
     *
     * @param useful
     * @return
     */
    @Select(" select vehi.vehicle_id, vehi.vehicle_cph, vehi.vehicle_cate, vehi.vehicle_type, vehi.vehicle_frame, " +
            " vehi.vehicle_factory, vehi.vehicle_model, vehi.vehicle_status, vehi.create_time, vehi.remark, vehi.production_date," +
            " emp.emp_id, emp.emp_name, emp.emp_phone " +
            " from vehicle_info vehi" +
            " join emp_vehicle_relation empvehi on empvehi.vehicle_id = vehi.vehicle_id" +
            " join emp_info emp on emp.emp_id = empvehi.emp_id " +
            " where vehi.useful = #{useful} and vehi.create_company = #{company_id} and empvehi.useful = 1  " +
            " order by create_time desc ")
    List<VehicleInfoVB> selectVehicleInfoVBByUseful(@Param("useful") String useful, @Param("company_id") String company_id);

    /**
     * 查询 车辆信息
     *
     * @param vehicle_id
     * @return
     */
    @Select("select vehicle_id, vehicle_cph, vehicle_cate, vehicle_type, vehicle_frame, " +
            " vehicle_factory, vehicle_model, vehicle_status, create_time, remark " +
            " from vehicle_info " +
            " where useful = 1 and vehicle_id = #{vehicle_id} " +
            " order by create_time desc ")
    VehicleInfoVB selectVehicleInfoVBByVehicleId(@Param("vehicle_id") String vehicle_id);

    /**
     * 查询 车辆id --- empId
     *
     * @param emp_id
     * @return
     */
    @Select(" select vehicle_id from emp_vehicle_relation where emp_id = #{emp_id} and useful = 1 ")
    List<String> selectVehicleIdsByEmpId(@Param("emp_id") String emp_id);

    /**
     * 更新 车辆信息
     *
     * @param vehicleInfoVB
     * @return
     */
    @Update("update vehicle_info set " +
            " vehicle_cph = #{vehicleInfoVB.vehicle_cph}, vehicle_cate = #{vehicleInfoVB.vehicle_cate}, vehicle_type = #{vehicleInfoVB.vehicle_type}, vehicle_frame = #{vehicleInfoVB.vehicle_frame}, vehicle_factory = #{vehicleInfoVB.vehicle_factory}, " +
            " vehicle_model = #{vehicleInfoVB.vehicle_model}, vehicle_status = #{vehicleInfoVB.vehicle_status}, remark = #{vehicleInfoVB.remark}, production_date = #{vehicleInfoVB.production_date}," +
            " update_user = #{user_id}, update_time = now() " +
            " where vehicle_id = #{vehicleInfoVB.vehicle_id} ")
    boolean updateVehicleInfo(@Param("vehicleInfoVB") VehicleInfoVB vehicleInfoVB, @Param("user_id") String user_id);

    /**
     * 更新 指定车辆的人车关系为无效
     *
     * @param vehicle_id
     * @return
     */
    @Update(" update emp_vehicle_relation set " +
            " useful = 0" +
            " where vehicle_id = #{vehicle_id} and useful = 1 ")
    boolean updateEmpVehicleRelation(@Param("vehicle_id")String vehicle_id);

    /**
     * 删除 重复的人员和车的对应关系
     *
     * @return
     */
    @Delete(" delete from emp_vehicle_relation where id not in( " +
            " select temp.id from ( " +
            " select * from emp_vehicle_relation GROUP BY emp_id, vehicle_id, useful " +
            " )temp) ")
    boolean distinctEmpVehicleRelation();


    /**
     * 更新 车辆信息无效
     *
     * @param vehicle_id
     * @return
     */
    @Update(" update vehicle_info set useful = 0, update_user = #{user_id}, update_time = now() where vehicle_id = #{vehicle_id} ")
    boolean updateVehicleInfoUsefulWx(@Param("vehicle_id") String vehicle_id, @Param("user_id") String user_id);

}
